//
//  MKNewFeatureCollectionViewCell.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/8/22.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit

class MKNewFeatureCollectionViewCell: UICollectionViewCell {
    
    var index: Int = 0 {
        
        didSet {
            
           imageView = UIImageView(image: UIImage(named: "new_feature_\(index+1)"))
            startButton.isHidden = true
        }
        
    }
    
    override init(frame: CGRect) {
     
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    private func prepareUI () {
    
    contentView.addSubview(imageView)
    contentView.addSubview(startButton)
        
    imageView.snp.makeConstraints { (mark) in
        mark.edges.equalTo(self.contentView)
        
        }
        startButton.snp.makeConstraints { (mark) in
            mark.centerX.equalTo(self.contentView)
            mark.bottom.equalTo(self.contentView).offset(-110)
        }
    }
    
    /// 开始按钮动画
    func startAnimation() {
        // 动画之前,将按钮显示出来
        startButton.isHidden = false
        // 在动画前设置transform.scale为0.看不到
        startButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 恢复到原来的大小
            self.startButton.transform = CGAffineTransform(scaleX: 1, y: 1) // 清空transform到默认的状态 scale = 1
            }, completion: nil)
    }
    
    func startButtonDidClick() {
        // 切换到CZMainTabBarController
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.switchRootViewController(controller: MKMainViewController())
    }
    
    
    private lazy var imageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        
       let btn = UIButton()
       btn.setTitle("开始体验", for: UIControlState.normal)
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
   btn.setBackgroundImage(UIImage(named:"new_feature_finish_button"), for: UIControlState.normal)
    btn.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), for: UIControlState.highlighted)
       
       btn.isHidden = true
        
        btn.addTarget(self, action: #selector(startButtonDidClick), for: UIControlEvents.touchUpInside)
        
       return btn
    }()

}
